/*
 * main.c
 *
 *  Created on: Nov 25, 2012
 *      Author: andrey
 */

#include "misc.h"

#include <stm32f4xx_gpio.h>
#include <stm32f4xx_rcc.h>
#include "my_lib.h"
#include "rficonsole.h"
#include "lcd_SSD1289.h"
#include "drawing.h"

#include "rficonsole.h"
#include "rficonsole_stm32.h"

#define GPIOD_LED4              12      /* Green LED.   */
#define GPIOD_LED3              13      /* Orange LED.  */
#define GPIOD_LED5              14      /* Red LED.		also FSMC_D0 */
#define GPIOD_LED6              15      /* Blue LED.    also FSMC_D1 */

#ifndef PAL_PORT_BIT
#define PAL_PORT_BIT(n) ((1 << (n)))
#endif

int main(void) {
	SysTick_Config(SystemCoreClock / 1000); // 1 ms in each tick

	mySTM_EVAL_LEDInit(RCC_AHB1Periph_GPIOD, PAL_PORT_BIT(GPIOD_LED3), GPIOD );
	mySTM_EVAL_LEDInit(RCC_AHB1Periph_GPIOD, PAL_PORT_BIT(GPIOD_LED4), GPIOD );

	turnPinOn(GPIOD, PAL_PORT_BIT(GPIOD_LED3)); /* Orange.  */
	turnPinOn(GPIOD, PAL_PORT_BIT(GPIOD_LED4)); /* Blue.  */

	LCD_SSD1289_Init();
	LCD_Clear(LCD_COLOR_BLUE);

	LCD_SetFont(&Font12x12);
	LCD_SetTextColor(LCD_COLOR_WHITE);
	LCD_SetBackColor(LCD_COLOR_BLUE2);
	LCD_DisplayStringLine(0, 0, VERSION_STRING);

	initConsole();

	while (1) {
		Delay(100);
		turnPinOn(GPIOD, PAL_PORT_BIT(GPIOD_LED4)); /* Blue.  */
		Delay(100);
		turnPinOff(GPIOD, PAL_PORT_BIT(GPIOD_LED4)); /* Green.  */

		if(hasStmConsoleLine()) {
			print("Still alive [%s]\r\n", getStmConsoleLine());
			handleConsoleLine(getStmConsoleLine());
			clearConsoleLine();
		}
	}
	return 0;
}
